import Vue from 'vue';
import axios from "axios";
const state = {
    isType: 0,//0为个定订单，1为我的模板
    Order_user_category_id: {},//形式为：{服装类别:1,服装品类:2....}
    Order_user_gongyiIDs: {},//用于存储服装类别和工艺选中数据，形式为：{领口款式:1,马甲款式:2....}
    Order_user_Info_temp: {
        token:localStorage.getItem("token"),//用户token
        id:'',//产品id
        orderSn: '',//订单编号
        templateSn: '',//模板编号
        productName:'',//产品名称
        categoryIds:'',//类别ids,多个以逗号隔开如:54,56,58,60
        fabricIds:'',//	面料ids,多个以逗号隔开面料ids:1/10,2/5跟以上相同：1表示面料id,10表示输入的米数
        buttonsIds:'',//纽扣id，目前就传一个id
        libuId:'',//里布id，目前就传一个id
        dilinliId:'',//低领里id，目前就传一个id
        bokuan:'',//驳款id
        zhubianWidth:'',//珠边宽度id
        xiuziPriceId:'',//绣字高度id
        xiuziType:'',//绣字类型
        xiuziWeizhi:'',//绣字位置
        xiuziImg:'',//绣字图片Id
        xiuziColor:'',//绣字颜色id
        xiuziHeight:'',//绣字具体高度
        zidingyiImg:'',//自定义图片
        xiuziZiti:'',//绣字字体id
        xiuziOneNeirong:'',//绣字第一行内容
        xiuziTwoNeirong:'',//绣字第二行内容
        zhuangseIds:'',//撞色id，多个以逗号隔开
        bodySizeIds:'',//身体尺寸ids:1/56,2/36以这种形式：1代表尺寸id,56代表尺寸1输入的值
        sizeIds:'',//具体的体型Ids:1,2,3
        liangtiRemark:'',//体型备注
        liangtiImgs:'',//量体图片，多个以逗号隔开
        remark:'',//产品备注
        fabricRemark:'',//特殊面料备注
    },
    Order_user_InfoData: {
        token:localStorage.getItem("token"),
        addressPerson:'',//地址联系人
        addressPhone:'',//联系电话
        addressType:'',//地址类型
    },
    EditCheckedData: {
        'plbx':{},//服装品类版型
        'ksgy':{},//服装类别和工艺
    },//继续填写或修改时，选中状态复位数据
    EditFabricIds: {},//面料选中状态/复位存储
    EditbodySizeIds: {},//量体尺寸选中状态/复位存储
    EditsizeIds: {},//量体体型选中状态/复位存储
    EditzhuangseIds: {},//撞色选中状态/复位存储
}

const actions = {
    setProductInfo({commit,rootState},proIndex) {
        commit('setEditChecked',proIndex);
        commit('getProductEditInfo',{'rootState':rootState,'proIndex':proIndex});
    },
    addOrderUser({commit,rootState}) {
        commit('getProductEditInfo',{'rootState':rootState,'proIndex':0});
    },
    addProduct({commit},id) {
        commit('addProduct',id);
    },
    editProduct({commit},id) {
        commit('editProduct',id);
    }
}

const mutations = {
    postOrder_user_product_update(state,status) {//保存产品函数，依靠个订首页setInterval触发
        if(state.Order_user_Info_temp.bodySizeIds!=null && state.Order_user_Info_temp.bodySizeIds!=''){
            state.Order_user_Info_temp.bodySizeIds='';
        }
        //#region 基本信息
        let keys = ['token','orderSn','storeId','storeCode','orderName','customerCode','customerName','customerSex','fabricSource','businessSource','courierId','packageType','addressType','address','thirdSn',
        'addressPerson','addressPhone'];
        let p1 = new FormData();
        if(state.Order_user_InfoData.addressType == '其他地址') {
            state.Order_user_InfoData.address = state.Order_user_InfoData.address2;
        }
        p1.append('status',status);
        keys.map(key => {
            if(!!state.Order_user_InfoData[key]) {
                p1.append(key,state.Order_user_InfoData[key]);
            }
        });
        // axios.post("http://clound_clothing.wenkangkeji.com/mlcc/" + 'logic/interface/update_single_order',p1).then();
        axios.post(domain.publicUrl + '/logic/interface/update_single_order',p1).then();
        //#endregion
        state.Order_user_Info_temp.categoryIds = this.getters.getOrder_user_category_id();
        state.Order_user_Info_temp.bodySizeIds = this.getters.getbodySizeIds;
        state.Order_user_Info_temp.sizeIds = this.getters.getsizeIds;
        let params = new FormData();
        for(let key in state.Order_user_Info_temp) {
            if(!!state.Order_user_Info_temp[key]) {
                params.append(key,state.Order_user_Info_temp[key]);
            }
        }

        // axios.post("http://clound_clothing.wenkangkeji.com/mlcc/" + 'logic/interface/update_products',params).then();
        axios.post(domain.publicUrl + '/logic/interface/update_products',params).then();
    },
    getProductEditInfo(state,arrObj) {
        let {rootState,proIndex} = arrObj;
        for(let key in state.Order_user_Info_temp) {
            if(!!state.Order_user_InfoData.productInfoList[proIndex]) {
                if(state.Order_user_InfoData.productInfoList[proIndex].hasOwnProperty(key)) {
                    state.Order_user_Info_temp[key] = state.Order_user_InfoData.productInfoList[proIndex][key];
                }
            }
        }
        if(state.Order_user_InfoData.addressType == '其他地址') {
            state.Order_user_InfoData.address2 = state.Order_user_InfoData.address;
            //还原门店地址
            let p = new FormData();
            p.append('store_code',state.Order_user_InfoData.storeId);
            // axios.post("http://clound_clothing.wenkangkeji.com/mlcc/" + 'logic/interface/query_storeinfoBycode',p).then(result => {
            //     state.Order_user_InfoData.address = result.data.data.storeAddress;
            // });
            axios.post(domain.publicUrl + '/logic/interface/query_storeinfoBycode',p).then(result => {
                state.Order_user_InfoData.address = result.data.data.storeAddress;
            });
        }
        // state.Order_user_Info_temp.token = rootState.token;
        state.Order_user_Info_temp.token = localStorage.getItem("token");
        state.Order_user_Info_temp.id = state.Order_user_InfoData.productInfoList[proIndex].id;
        state.Order_user_Info_temp.orderSn = state.Order_user_InfoData.orderSn;
        state.Order_user_Info_temp.categoryIds = this.getters.getOrder_user_category_id();
    },
    setEditChecked(state,proIndex) {
        //从订单详情提取产品信息，记录在EditCheckedData，作为选中状态复位使用。
        let data = state.Order_user_InfoData.productInfoList[proIndex];
        //品类版型工艺plbx
        if(!!data.plbx) {
            state.EditCheckedData.plbx = {};
            for(let x of data.plbx) {
                Vue.set(state.EditCheckedData.plbx,x.smallTemplateName,x.id);
            }
        }
        //服装类别和工艺
        if(!!data.ksgy) {
            state.EditCheckedData.ksgy = {};
            for(let x of data.ksgy) {
                Vue.set(state.EditCheckedData.ksgy,x.smallTemplateName,x.id);
            }
        }
        //撞色
        if(!!data.allConColor) {
            state.EditzhuangseIds = {};
            for(let x of data.allConColor) {
                Vue.set(state.EditzhuangseIds,x.id,x.colorName);
            }
        }
        //量体信息 -- 尺寸
        if(!!data.bodySizeIds) {
            state.EditbodySizeIds = {};
            let temp = data.bodySizeIds.toString();
            if(temp.indexOf(',') > -1) {
                for(let id of temp.split(',')) {
                    let list = data.productBodySizeList;
                    for(let item of list) {
                        if(item.bodysizeId == id) {
                            Vue.set(state.EditbodySizeIds,item.bodysizeId,item.bodysizeValue);
                        }
                    }
                }
            }else {
                //只有一个
                let list = data.productBodySizeList;
                for(let item of list) {
                    if(item.bodysizeId == temp) {
                        Vue.set(state.EditbodySizeIds,item.bodysizeId,item.bodysizeValue);
                    }
                }
            }
        }
        //量体信息 -- 体型数据
        if(!!data.sizeIds) {
            state.EditsizeIds = {};
            let temp = data.sizeIds.toString();
            if(temp.indexOf(',') > -1) {
                for(let id of temp.split(',')) {
                    let list = data.sizeInfoList;
                    for(let item of list) {
                        if(item.id == id) {
                            Vue.set(state.EditsizeIds,item.sizeName,item.id);
                        }
                    }
                }
            }else {
                //只有一个
                let list = data.sizeInfoList;
                for(let item of list) {
                    if(item.id == temp) {
                        Vue.set(state.EditsizeIds,item.sizeName,item.id);
                    }
                }
            }
        }
    },
    saveOrderUserTemplate(state,templateName = '') {//个订保存为模板
        let x = state.Order_user_InfoData;
        let pro = state.Order_user_Info_temp;
        let keys = {
            token: localStorage.getItem("token"),//用户token
            orderSn: pro.templateSn,//从创建订单那里获取
            templateName: templateName == '' ? x.templateName : templateName,//模板名称
            storeId: x.storeId,//门店id
            storeCode: x.storeCode,//门店编码
            mianliaoSource: x.fabricSource,//面料来源
            yewuType: x.businessSource,//业务类型
            packageType: x.packageType,//包装方式
            kuaidiId: x.courierId,//快递id
            addressType: x.addressType,//地址类型
            address:'',//收货地址
            thirdSn: x.thirdSn,//第三方运单号
            kuhuName: x.customerName,//客户名称
            kuhuCode: x.customerCode,//客户编码
            kuhuSex: x.customerSex,//客户性别
            categoryIds: this.getters.getOrder_user_category_id(),//类别id,多个以逗号隔开
            ksBokuan: pro.bokuan,//驳款
            ksZhubianWidth: pro.zhubianWidth,//珠边宽度
            xiuziType: pro.xiuziType,//绣字类型
            xiuziWeizhi: pro.xiuziWeizhi,//绣字位置
            xiuziImg: pro.xiuziImg,//绣字图片id
            xiuziColor: pro.xiuziColor,//绣字颜色id
            xiuziHeight: pro.xiuziHeight,//绣字高度
            xiuziPriceId: pro.xiuziPriceId,//绣字高度id
            zidingyiImg: pro.zidingyiImg,//自定义图片
            xiuziZiti: pro.xiuziZiti,//绣字字体id
            xiuziOneNeirong: pro.xiuziOneNeirong,//绣字第一行内容
            xiuziTwoNeirong: pro.xiuziTwoNeirong,//绣字第二行内容
            zhuangseId: pro.zhuangseIds,//撞色id,多个以逗号隔开
            bodySizeIds: this.getters.getbodySizeIds,//尺寸ids：1/5,2/90：1代表尺寸id，5表示尺寸1输入的值
            sizeIds: this.getters.getsizeIds,//具体的体型ids :1,2,3
            fabricIds: pro.fabricIds,//面料ids:1/10,2/5跟以上相同：1表示面料id,10表示输入的米数
            buttonsIds: pro.buttonsIds,//纽扣id，这里是单个
            liangtiRemark: pro.liangtiRemark,//体型备注
            liangtiImgs: pro.liangtiImgs,//量体图片，多个以逗号隔开
        };
        let p1 = new FormData();
        if(state.Order_user_InfoData.addressType == '其他地址') {
            keys.address = state.Order_user_InfoData.address2;
        }else {
            keys.address = state.Order_user_InfoData.address;
        }
        for(let key in keys) {
            p1.append(key,keys[key]);
        }
        // axios.post("http://clound_clothing.wenkangkeji.com/mlcc/" + 'logic/interface/save_sign_module',p1).then();
        axios.post(domain.publicUrl + '/logic/interface/save_sign_module',p1).then();
        //#endregion
    },
    addProduct(state,id) {//新建产品
        let {token,orderSn} = state.Order_user_InfoData;
        let {templateSn} = state.Order_user_Info_temp.templateSn;
        for(let item in state.Order_user_Info_temp) {
            state.Order_user_Info_temp[item] = undefined;
        }
        state.Order_user_Info_temp.token = token;
        state.Order_user_Info_temp.id = id;
        state.Order_user_Info_temp.orderSn = orderSn;
        state.Order_user_Info_temp.templateSn = templateSn;
        state.EditCheckedData.plbx = {};
        state.EditCheckedData.ksgy = {};
        state.EditzhuangseIds = {};
        state.EditbodySizeIds = {};
        state.EditsizeIds = {};
    }
}

const getters = {
    setOrder_user_InfoData: state => (value) => {
    },
    getOrder_user_category_id: state => (type = '') => {//获取处理后的category_ids
        //取出单选数据集合逗号拼接形式的参数
        let category_ids = '';
        if(type == '') {
            for(let x in state.EditCheckedData) {
                for(let key in state.EditCheckedData[x]) {
                    if(category_ids == '') {
                        category_ids = state.EditCheckedData[x][key];
                    }else {
                        category_ids += ',' + state.EditCheckedData[x][key];
                    }
                }
            }
        }else {
            for(let x in state.EditCheckedData[type]) {
                if(category_ids == '') {
                    category_ids = state.EditCheckedData[type][x];
                }else {
                    if(!!state.EditCheckedData[type][x]) {
                        category_ids += ',' + state.EditCheckedData[type][x];
                    }
                }
            }
        }
        return category_ids;
    },
    getOrder_user_gongyiIDs: state => {//获取处理后的服装类别和工艺选中数据
        let category_ids = '';
        for(let x in state.Order_user_gongyiIDs) {
            if(category_ids == '') {
                category_ids = state.Order_user_gongyiIDs[x];
            }else {
                category_ids += ',' + state.Order_user_gongyiIDs[x];
            }
        }
        return category_ids;
    },
    getbodySizeIds: state => {
        let arr = '';
        for(let x in state.EditbodySizeIds) {
            if(arr == '') {
                arr = x + '/' + state.EditbodySizeIds[x];
            }else {
                arr += ',' + x + '/' + state.EditbodySizeIds[x];
            }
        }
        return arr;
    },
    getsizeIds: state => {
        let sizeIds = '';
        for(let x in state.EditsizeIds) {
            if(sizeIds == '') {
                sizeIds = state.EditsizeIds[x];
            }else {
                sizeIds += ',' + state.EditsizeIds[x];
            }
        }
        return sizeIds;
    }
}

export default {state,actions,mutations,getters}
